
import java.awt.Point;

public class TalkTo implements State {
    
    private FSMachine m_fsm;
    private Point m_desPos;
    private int myID;
    private int otherID;
    
    public TalkTo(FSMachine fsm) {
        m_fsm = fsm;
        
    }
    
    public void Enter(int myID, int otherID) {
        this.myID = myID;
        this.otherID = otherID;
        if (!Global.agents.get(otherID).isLiving()) {
            Exit();
        }
    }
    
    public void Execute() {
        // Pseudo-code
        // call an API for moving an agent to a given destination. 
        // if(desPos != curPos)
        // {
        //      move to a destination
        // }
        // else 
        // {
        // 		talk to an agent
        //		m_fsm.ChangeState(FSMachine.enumState.GUESS);
        // }
        //ConversationEngine CE = new ConversationEngine("src/gData/Sentences.txt"); 
        //if there is something new in the table
        if (!Global.agents.get(otherID).myEvents.isEmpty()) {
            System.out.println("Hey! Something happened and I MUST tell you! " + otherID);
            //Gathers Evidence and tries to guess.
            if (Global.makeGuess(otherID) != -1) {
                System.out.println("I think I may have a clue man " + otherID);
            } //If there is no guess, then it will tell you about the latest he heard.       
            else {
                Event ev = Global.agents.get(otherID).myEvents.get(Global.agents.get(otherID).myEvents.size() - 1);
                ev.setWitnessid(otherID);
                ev.setType("Gossip");
                if (!Global.agents.get(myID).myEvents.contains(ev)) {
                    Global.agents.get(myID).myEvents.add(ev);
                    for (int i = 0; i < Global.agents.get(myID).getFriends().size(); i++) {
                        Global.agents.get(Global.agents.get(myID).getFriends().get(i)).myEvents.add(ev);
                    }
                } else {
                    System.out.println("I already told you that lol");
                }
            }
        } else {
            System.out.println("Agent " + myID + " had a casual conversation with " + otherID);
            //Small Talk
        }
        System.out.println("Agent " + myID + " talked to Agent " + otherID);

        // TODO talking logic goes here

        if (Global.agents.get(myID).isKiller()) {
            m_fsm.ChangeState(FSMachine.enumState.GUESS, myID, -1);
        } else {
            m_fsm.ChangeState(FSMachine.enumState.WANDER, myID, -1);
        }
        m_fsm.ChangeState(FSMachine.enumState.WANDER, otherID, -1);
    }
    
    public void Exit() {
        System.out.println("Agent " + myID + " finished talking to Agent " + otherID);
    }
    
    public void SetDesPos(Point desPos) {
        m_desPos = desPos;
    }
}
